package com.itheima.test;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class MyJFrame extends JFrame implements ActionListener {

    JButton jtb1=new JButton("点我");
    JButton jtb2=new JButton("别点我");
    public MyJFrame()
    {
        //宽高
        this.setSize(603,680);
        //标题
        this.setTitle("事件演示");
        //界面上浮
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //取消组件居中放置,取消了才会按xy轴的形式添加组件
        this.setLayout(null);

        //给按钮设置位置和宽高
        jtb1.setBounds(0,0,100,50);
        //给按钮添加事件
        jtb1.addActionListener(this);

        jtb2.setBounds(100,0,100,50);

        jtb2.addActionListener(this);


        //按钮添加界面中
        this.getContentPane().add(jtb1);
        this.getContentPane().add(jtb2);

        //使窗口显示
        this.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e)
    {
        //对当前的按钮进行判断
        //获取当前被操作的那个按钮对象
        Object source = e.getSource();

        if(source == jtb1)
        {
            jtb1.setSize(200,200);
        }
        else if(source == jtb2)
        {
            Random r=new Random();
            jtb2.setLocation(r.nextInt(500),r.nextInt(500));
        }
        System.out.println("按下按钮");
    }
}
